package com.example.jdai.jdai.ui;

import android.app.Application;
import android.app.Fragment;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.util.ArrayMap;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.example.jdai.jdai.MyApplication;
import com.example.jdai.jdai.R;
import com.example.jdai.jdai.config.Config;
import com.example.jdai.jdai.rest.BackRest;
import com.example.jdai.jdai.rest.GetData;
import com.example.jdai.jdai.rest.SendCode;
import com.example.jdai.jdai.threeparty.zm.DemoPresenterImpl;
import com.example.jdai.jdai.util.DataAll;
import com.example.jdai.jdai.util.DataConnect;
import com.example.jdai.jdai.util.DataJd;
import com.example.jdai.jdai.util.DataLnfo;
import com.example.jdai.jdai.util.DataPhone;
import com.example.jdai.jdai.util.DataZfb;
import com.example.jdai.jdai.util.DataZx;
import com.example.jdai.jdai.util.Tools;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.io.IOException;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;
import okhttp3.Callback;

public class CheckFragment extends Fragment implements InitData {


    @BindView(R.id.ll_info)
    LinearLayout llInfo;
    @BindView(R.id.ll_connect)
    LinearLayout llConnect;
    @BindView(R.id.ll_zima)
    LinearLayout llZima;
    //@BindView(R.id.ll_huabei)
    //LinearLayout llHuabei;
    @BindView(R.id.ll_phone)
    LinearLayout llPhone;
    @BindView(R.id.tv_info)
    MyTextTip tvInfo;
    @BindView(R.id.tv_connect)
    MyTextTip tvConnect;
    @BindView(R.id.tv_zima)
    MyTextTip tvZima;
    @BindView(R.id.tv_phone)
    MyTextTip tvPhone;
    @BindView(R.id.tv_zx)
    MyTextTip tvZx;
    @BindView(R.id.ll_zx)
    LinearLayout llZx;
    @BindView(R.id.tv_zfb)
    MyTextTip tvZfb;
    @BindView(R.id.ll_zfb)
    LinearLayout llZfb;
    @BindView(R.id.tv_jd)
    MyTextTip tvJd;
    @BindView(R.id.ll_jd)
    LinearLayout llJd;
    DemoPresenterImpl presenter;

    public CheckFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_check, container, false);
        ButterKnife.bind(this, view);
        initData();
        return view;
    }

    @OnClick({R.id.ll_info, R.id.ll_connect, R.id.ll_zima, R.id.ll_phone,R.id.ll_zfb, R.id.ll_jd,R.id.ll_zx})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.ll_info:
               // if(!DataAll.ifOk(DataAll.dataLnfo.data.get(DataLnfo.INFO_STATUS)))
                startActivity(new Intent(getActivity(), LnfoActivity.class));
                //else
               //okTip();
                break;
            case R.id.ll_connect:
                if(!DataAll.ifOk(DataAll.dataConnect.data.get(DataConnect.CT_STATUS)))
                startActivity(new Intent(getActivity(), ConnectActivity.class));
                else
                okTip();
                break;
            case R.id.ll_zima:
                //zima();
                if(!DataAll.ifOk(MyApplication.dataAll.data.get(DataAll.ZIMA_STATUS)))
                startActivity(new Intent(getActivity(),ZmWebActivity.class));
                else
                    okTip();
                break;
//            case R.id.ll_huabei:
//
//                break;
            case R.id.ll_phone:
                if(!DataAll.ifOk(DataAll.dataPhone.data.get(DataPhone.PHONE_CODE)))
                startActivity(new Intent(getActivity(), PhoneActivity.class));
                else
                    okTip();
                break;
            case R.id.ll_zx:
                startActivity(new Intent(getActivity(), ZxActivity.class));

                break;
            case R.id.ll_zfb:
                if(!DataAll.ifOk(DataAll.dataZfb.data.get(DataZfb.ZFB_STATUS)))
                startActivity(new Intent(getActivity(), TbActivity.class));
                else
                    okTip();
                break;
            case R.id.ll_jd:
                startActivity(new Intent(getActivity(), JdActivity.class));
                break;
        }
    }

    @Override
    public void initData() {
        if(Config.ISDEBUG)
            Log.v("init","cf");
        tvInfo.setZhuangtai(DataAll.dataLnfo.data.get(DataLnfo.INFO_STATUS));
        tvConnect.setZhuangtai(DataAll.dataConnect.data.get(DataConnect.CT_STATUS));
        tvPhone.setZhuangtai(DataAll.dataPhone.data.get(DataPhone.PHONE_CODE));
        tvZima.setZhuangtai(MyApplication.dataAll.data.get(DataAll.ZIMA_STATUS));
        tvZx.setZhuangtai(DataAll.dataZx.data.get(DataZx.ZX_STATUS));
        tvZfb.setZhuangtai(DataAll.dataZfb.data.get(DataZfb.ZFB_STATUS));
        //tvJd.setZhuangtai(DataAll.dataJd.data.get(DataJd.BT_STATUS));
    }

    @Override
    public void onResume() {
        if(Config.ISDEBUG)
            Log.v("cf","sss");
        super.onResume();
      //  MyApplication.backRest.getInfo(getActivity());
        initData();
        tip();
    }
    void tip(){
        if(MyApplication.dataAll.checkCanUp()) {
            AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
            //    设置Title的图标
            builder.setIcon(R.drawable.logo1);
            builder.setTitle("温馨提示");
            builder.setMessage("您的数据已经填写完成,是否提交？");
            //    设置一个PositiveButton
            builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    if (DataAll.checkAll()) {
                        ((MainActivity)getActivity()).commit();

                    } else {
                        Toast.makeText(getActivity(), "必填项未填写完成", Toast.LENGTH_SHORT).show();
                    }

                }
            }).show();
        }

    }


    void zima() {
        //startActivity(new Intent(getActivity(),ZmWebActivity.class));
        SendCode send = new SendCode();
       send.data.put("tel", MyApplication.dataAll.data.get(DataAll.HAS_LOGIN));
        send.data.put("user_id", MyApplication.dataAll.data.get(DataAll.LOGIN_ID));
        Call call = MyApplication.okClient.newCall(Tools.getRequest(Config.ZIMA_GET, send));
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }
            @Override
            public void onResponse(Call call, okhttp3.Response response) throws IOException {
                int code = response.code();
                String bd = response.body().string();
                if (Config.ISDEBUG)
                    Log.v("zm_get", bd);
                if (200 == code) {
                    // Type jsonType = new TypeToken<GetData<LoginBean>>(){}.getType();
                    GetData<Map<String,String>> zima = new Gson().fromJson(bd, new TypeToken<GetData<Map<String,String>>>() {
                    }.getType());
//                    Map str=new ArrayMap();
//                    str.put("params",zima.getData().get("params"));
//                    str.put("sign",zima.getData().get("sign"));
                    initZima(zima.getData().get("params"),zima.getData().get("sign"));
                    presenter.doCreditRequest();

                    //String zima = zima.getData().getVerify_info().getZmxy_verify();
                    //MyApplication.dataAll.data.put(DataAll.ZIMA_STATUS,zima);
                    // Tools.saveData(loginBean.getData().getVerify_info());

                }
            }

        });

    }

    void zihubao() {
        //AlipaySDK

    }
    //字码初始化
    void initZima(String par,String sign){
        presenter = new DemoPresenterImpl(getActivity(),par,sign);
        presenter.setTipDone(new DemoPresenterImpl.TipDone() {
            @Override
            public void ok(String openid) {
                MyApplication.backRest.ZimaOk(getActivity(),openid);
                initData();
            }

            @Override
            public void cancle() {
                Toast.makeText(getActivity(), "授权取消", Toast.LENGTH_SHORT).show();
                //MyApplication.dataAll.data.put(DataAll.ZIMA_STATUS, "");
            }

            @Override
            public void error() {
                Toast.makeText(getActivity(), "授权错误", Toast.LENGTH_SHORT).show();
                MyApplication.dataAll.data.put(DataAll.ZIMA_STATUS, "");
            }
        });
    }
    void okTip(){
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        //    设置Title的图标
        builder.setIcon(R.drawable.logo1);
        builder.setTitle("温馨提示");
        builder.setMessage("该项信息已经完善或审核通过！");
        //    设置一个PositiveButton
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {

            }
        }).show();
    }

}
